/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.viewlists;

import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import listo.client.taskfilters.TaskFilter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public abstract class ViewList extends JList implements TaskFilter.Listener {
    protected List<View> views;

    @Inject
    public ViewList(final ViewListManager viewListManager, ViewListRenderer renderer, TransferHandler transferHandler,
                    TaskFilter filter) {
        setBackground(SystemColor.control);
        setCellRenderer(renderer);

        filter.addFilterListener(this);

        if (transferHandler != null) {
            setDragEnabled(true);
            setDropMode(DropMode.ON);
            setTransferHandler(transferHandler);
        }

        viewListManager.register(this);

        addKeyListener(new KeyAdapter() {
            @SuppressWarnings("fallthrough")
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP:
                    case KeyEvent.VK_PAGE_UP:
                        if (getSelectedIndex() != 0) return;

                    case KeyEvent.VK_HOME:
                        ViewList previousList = viewListManager.getPreviousList(ViewList.this);
                        if (previousList != null) previousList.requestFocusInWindow();
                        break;

                    case KeyEvent.VK_DOWN:
                        if (getSelectedIndex() != getModel().getSize() - 1) return;

                    case KeyEvent.VK_END:
                        ViewList nextList = viewListManager.getNextList(ViewList.this);
                        if (nextList != null) nextList.requestFocusInWindow();
                        break;

                    default:
                        return; // don't consume
                }
                e.consume();
            }
        });

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) createPopupMenu(e);
            }

            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) createPopupMenu(e);
            }
        });
    }

    protected abstract void createPopupMenu(MouseEvent e);

    public void setListData(View[] listData) {
        views = ImmutableList.of(listData);
        super.setListData(listData);
    }

    public View getViewAt(int index) {
        if (index < 0 || index >= views.size()) return null;
        return views.get(index);
    }

    public int indexOf(View view) {
        return views.indexOf(view);
    }

}
